package org.ddd.book.infrastructure.eventpublisher;

import com.google.common.eventbus.AsyncEventBus;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ddd.book.domain.IBaseEvent;
import org.ddd.book.domain.IEventPublisher;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * EventBus领域事件发布器实现（为了突出对比，此demo提供了MQEventPublisher作参考）
 */
@Component("EventBusEventPublisher")
@AllArgsConstructor
@Primary
@Slf4j
public class EventBusEventPublisher implements IEventPublisher {

    @SuppressWarnings("all")
    private final AsyncEventBus eventBus;

    @Override
    public void publishEvent(IBaseEvent event) {
        postEvent(event);
        log.info("EventBus publish domain event!");
    }

    @SuppressWarnings("all")
    private void postEvent(IBaseEvent event) {
        eventBus.post(event);
    }

}
